# -*- coding: utf-8 -*-
"""
Created on Sat Nov  2 14:03:54 2019

@author: haodong
"""

import numpy as np
import pandas as pd

from sklearn.linear_model import Lasso,Ridge,ElasticNet
#导入数据
df=pd.read_csv('diabetes.csv')
features=list(df.columns)
features.remove('y')
labels = ["y"]
#用lasso求解
lamb=0.5
lasso_reg=Lasso(alpha=lamb)
#对10个原始自变量做回归
lasso_reg.fit(df[features[1:11]],df[labels])
incercept=lasso_reg.intercept_
coef=lasso_reg.coef_


#对比岭回归
lamb=0.1
Ridge_reg=Ridge(alpha=lamb)
Ridge_reg.fit(df[features[1:11]],df[labels])
incercept=Ridge_reg.intercept_
coef=Ridge_reg.coef_.T


#引入全部二次项后做回归
lamb=0.1
lasso_reg2=Lasso(alpha=lamb)
lasso_reg2.fit(df[features],df[labels])
incercept2=lasso_reg2.intercept_
coef2=lasso_reg2.coef_

#改用弹性网络方法
lamb=0.1
ElasticNet_reg=ElasticNet(alpha=lamb,l1_ratio=0.95)
ElasticNet_reg.fit(df[features],df[labels])
incercept3=ElasticNet_reg.intercept_
coef3=ElasticNet_reg.coef_.T


#超参数选择
from sklearn.linear_model import LassoCV,RidgeCV,ElasticNetCV
lasso_reg=LassoCV(cv=20).fit(df[features],df[labels])
incercept4=lasso_reg.intercept_
coef4=lasso_reg.coef_
lamb4=lasso_reg.alpha_
